#include <iostream>
#include <string>
#include <sstream>

#include <map>
#include <memory>
class Service {
public:
    Service() : serviceName("unknown"), isRunning(false), maxConnections(0) {}
    Service(const std::string& name) : serviceName(name), isRunning(false), maxConnections(0) {}

    void start() {
        isRunning = true;
        std::cout << serviceName << " started.\n";
    }

    void stop() {
        isRunning = false;
        std::cout << serviceName << " stopped.\n";
    }

    void status() const {
        std::cout << serviceName << (isRunning ? " is running.\n" : " is stopped.\n");
    }

    void setMaxConnections(int max) {
        maxConnections = max;
        std::cout << "MaxConnections for " << serviceName << " set to " << maxConnections << ".\n";
    }

private:
    std::string serviceName;
    bool isRunning;
    int maxConnections;
};


// 上下文类
class Context {
    std::map<std::string, Service> services;
public:
    Context() {
        // 初始化一些服务
        services["nginx"] = Service("nginx");
        services["apache"] = Service("apache");
        services["mysql"] = Service("mysql");
        services["httpd"] = Service("httpd");
    }

    void startService(const std::string& name) {
        if (services.find(name) != services.end()) {
            services[name].start();
        } else {
            std::cout << "Service " << name << " not found.\n";
        }
    }

    void stopService(const std::string& name) {
        if (services.find(name) != services.end()) {
            services[name].stop();
        } else {
            std::cout << "Service " << name << " not found.\n";
        }
    }

    void serviceStatus(const std::string& name) const {
        if (services.find(name) != services.end()) {
            services.at(name).status();
        } else {
            std::cout << "Service " << name << " not found.\n";
        }
    }

    void setServiceMaxConnections(const std::string& name, int max) {
        if (services.find(name) != services.end()) {
            services[name].setMaxConnections(max);
        } else {
            std::cout << "Service " << name << " not found.\n";
        }
    }
};

// 抽象表达式接口
class Expression {
public:
    virtual void interpret(Context& context) = 0;
    virtual ~Expression() {}
};

// 具体的命令表达式
class StartCommand : public Expression {
    std::string service;
public:
    StartCommand(const std::string& service) : service(service) {}

    void interpret(Context& context) override {
        context.startService(service);
    }
};

class StopCommand : public Expression {
    std::string service;
public:
    StopCommand(const std::string& service) : service(service) {}

    void interpret(Context& context) override {
        context.stopService(service);
    }
};

class StatusCommand : public Expression {
    std::string service;
public:
    StatusCommand(const std::string& service) : service(service) {}

    void interpret(Context& context) override {
        context.serviceStatus(service);
    }
};

class SetMaxConnectionsCommand : public Expression {
    std::string service;
    int maxConnections;
public:
    SetMaxConnectionsCommand(const std::string& service, int max) 
        : service(service), maxConnections(max) {}

    void interpret(Context& context) override {
        context.setServiceMaxConnections(service, maxConnections);
    }
};

// 客户端解析和执行类
class Client {
public:
    void executeScript(const std::string& script, Context& context) {
        auto expr = parseScript(script);
        if (expr) {
            expr->interpret(context);
        } else {
            std::cout << "Unknown command.\n";
        }
    }

    std::unique_ptr<Expression> parseScript(const std::string& script) {
        // 简单的解析器，假设命令是以空格分隔的
        std::istringstream iss(script);
        std::string command, service;
        iss >> command >> service;

        if (command == "start") {
            return std::make_unique<StartCommand>(service);
        } else if (command == "stop") {
            return std::make_unique<StopCommand>(service);
        } else if (command == "status") {
            return std::make_unique<StatusCommand>(service);
        } else if (command == "set") {
            std::string param;
            int value;
            iss >> param >> value;
            if (param == "MaxConnections") {
                return std::make_unique<SetMaxConnectionsCommand>(service, value);
            }
        }

        return nullptr;
    }
};

// 测试用例
int main() {
    Context context;
    Client client;

    // 测试脚本执行
    client.executeScript("start nginx", context);
    client.executeScript("status nginx", context);
    client.executeScript("set httpd MaxConnections 100", context);
    client.executeScript("status apache", context);
    client.executeScript("stop nginx", context);
    client.executeScript("status nginx", context);

    return 0;
}
